/*
 * Copyright (c) 2000-2019 TeamDev Ltd. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */
package samples;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserCore;
import com.teamdev.jxbrowser.chromium.Callback;
import com.teamdev.jxbrowser.chromium.FocusListener;
import com.teamdev.jxbrowser.chromium.PopupContainer;
import com.teamdev.jxbrowser.chromium.PopupHandler;
import com.teamdev.jxbrowser.chromium.PopupParams;
import com.teamdev.jxbrowser.chromium.internal.Environment;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import java.awt.Rectangle;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.layout.BorderPane;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * The sample demonstrates how to receive a request to focus the popup window from JavaScript.
 */
public class JavaFXFocusSample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {
        // On Mac OS X Chromium engine must be initialized in non-UI thread.
        if (Environment.isMac()) {
            BrowserCore.initialize();
        }
    }

    @Override
    public void start(final Stage primaryStage) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);
        primaryStage.setTitle("JxBrowser - Focus test (JavaFX)");
        Scene scene = new Scene(new BorderPane(view), 800, 800);
        primaryStage.setScene(scene);
        primaryStage.show();

        browser.setPopupHandler(new MyPopupHandler());

        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            @Override
            public void invoke(Browser browser) {
                browser.loadHTML("<button onclick=\"popup.focus();\">Focus</button>");
            }
        });
        browser.executeJavaScript("var popup = window.open('https://google.com')");
    }

    private static class MyPopupHandler implements PopupHandler {
        @Override
        public PopupContainer handlePopup(PopupParams params) {
            return new PopupContainer() {
                public void insertBrowser(final Browser browser, final Rectangle initialBounds) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            final Stage stage = new Stage();
                            stage.setX(0);
                            stage.setY(0);
                            stage.setWidth(800);
                            stage.setHeight(800);
                            StackPane root = new StackPane();
                            Scene scene = new Scene(root);
                            BrowserView browserView = new BrowserView(browser);
                            root.getChildren().add(browserView);
                            stage.setScene(scene);

                            browser.addFocusListener(new FocusListener() {
                                @Override
                                public void onFocusRequested() {
                                    Platform.runLater(new Runnable() {
                                        public void run() {
                                            stage.toFront();
                                        }
                                    });
                                }
                            });
                            stage.show();
                        }
                    });
                }
            };
        }
    }
}